let http = require("http")
let context = require("./context")
class Application {
    constructor() {
        this.cb;
        // 继承指定对象
        this.context = Object.create(context)
    }
    createContex(req, res) {
        let ctx = this.context;
        ctx.req = req;
        ctx.res = res;
        return ctx;
    }
    callback() {
        return (req, res) => {
            let ctx = this.createContex(req, res)
            this.cb(ctx)
            this.responseBody(ctx)
        }
    }
    responseBody(ctx) {
        let content = ctx.body;
        ctx.res.end(content)
    }
    listen(port) {
        let app = http.createServer(this.callback())
        app.listen(port)
    }
    use(fn) {
        this.cb = fn
    }
}

module.exports = Application;